//#include <stdio.h>
#include "jdt.h"
#include <unistd.h>
#include <string.h>
#define FH '='
#define NUM 101

void text_1()
{
    char buffer[NUM] = { 0 };
    int i = 1;
    const char* ha = "|/~\\";
    int j = 0;
    while(i <= 100)
    {
        j = i % 4;
        buffer[i-1] = FH;
        printf("[%-100s][%d%%][ %c ]\r", buffer, i, ha[j]);
        fflush(stdout);
        usleep(200000);
        i++;
    }
        printf("\n");
}



//单次打印
void text_2(double total, double current, double speed)
{
    char buffer[NUM];
    memset(buffer,0,sizeof(buffer));
    double x = (current / total) * 100;
    int i = (int)x;
    char* ha = "|/~\\";
    int j = 0;
    j = i % 4;
    int h = 0;
    while(2 * h < i)
    {
        buffer[h++] = FH;
    }
    //printf("[%-50s][%.0lfMB/%.0lfMB][%.2lfMB/s][%.1lf%%][%c]\r", buffer, current, total, speed, x, ha[j]);
    printf("[%-50s][%.0lfMB/%.0lfMB][%.2lfMB/s][%.1lf%%][", buffer, current, total, speed, x);
    fflush(stdout);
    for (int i = 0; i < 8; i++)
    {
        printf("%c]\b\b", ha[i % 4]);
        fflush(stdout);
        usleep(125000);
    }
    printf("\r");
    fflush(stdout);    
}


void Download()
{
    // 使用当前时间作为种子初始化随机数生成器
    srand((unsigned int)time(NULL));
    double x = (rand() % (400 + 1)) + 100;
    double y = 0;
    double speed = (rand() % 10000) / 1000.0;
    while(y < x + speed)
    {
        speed = (rand() % 10000) / 1000.0;
        if (y > x)
        {
            text_2(x, x, speed);
        }
        else
        {
            text_2(x, y, speed);
        }
       // usleep(20000);
        y += speed;
    }
    printf("\n");
}


